/*
 *  Copyright 2010 asustatil.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.userinterface.impl;

import edu.upf.core.User;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author asustatil
 */
public class ContactListCellRenderer extends JLabel implements ListCellRenderer{

    private ImageIcon online;
    private ImageIcon offline;
    private final int ICON_TEXT_GAP=8;

    public ContactListCellRenderer() {
        setOpaque(true);
        setIconTextGap(ICON_TEXT_GAP);
        online = new ImageIcon(getClass().getResource("/list_icons/available.png"));
        offline = new ImageIcon(getClass().getResource("/list_icons/unavailable.png"));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {

        User entry = (User) value;
        setText(entry.getName());
        if(entry.isAvailable()){
            setIcon(online);
        }else{
            setIcon(offline);
        }
        setBackground(isSelected
                ? list.getSelectionBackground() : list.getBackground());
        setForeground(isSelected
                ? list.getSelectionForeground() : list.getForeground());
        return this;
    }
}
